import { ConfigModuleOptions } from '@nestjs/config';
import * as dotenv from 'dotenv';
import dotenvExpand from 'dotenv-expand';
import * as fs from 'fs';
import { resolve } from 'path';

class File {
  static loadEnvFile(options: ConfigModuleOptions): Record<string, any> {
    const envFilePaths = Array.isArray(options.envFilePath)
      ? options.envFilePath
      : [options.envFilePath || resolve(process.cwd(), '.env')];
    console.log(envFilePaths, '===>>');
    let config: ReturnType<typeof dotenv.parse> = {};
    for (const envFilePath of envFilePaths) {
      if (fs.existsSync(envFilePath)) {
        config = Object.assign(config, dotenv.parse(fs.readFileSync(envFilePath)));
        if (options.expandVariables) {
          config = dotenvExpand({ parsed: config }).parsed || config;
        }
      }
    }
    return config;
  }
}

const file = File.loadEnvFile({
  envFilePath: ['.env.development.local', '.env.development'],
  isGlobal: true,
  ignoreEnvFile: true,
});
console.log(file);
